import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from "view/user/Login";
import HomeLayouts from "@/layouts/HomeLayouts";
import NotFound from "view/404";
import welcome from "view/welcome";
import store from "@/store"

Vue.use(VueRouter)

const routes = [
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'HomeLayouts',
    component: HomeLayouts,
    redirect:"/welcome",
    children: [      
      {
        name: "welcome",
        path: "/welcome",
        component: welcome
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('view/user/users')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('view/right/Roles')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('view/right/Rights')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('view/good/Categories')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('view/good/Goods')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('view/good/Params')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('view/order/Orders')
      },


      {
        name: "NotFound",
        path: "*",
        component: NotFound
      },
    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

//路由守卫
router.beforeEach((to,from,next) => {
  const token =  store.getters.token; 
  const isLogin = to.name === "Login"
  //如果有token 并且在登录页 那就跳转到首页
  if(token && isLogin){
    return next({name:"HomeLayouts"})
  }
  //如果没有 token 并且不在登录页 那就跳转到首页
  if(!token && !isLogin){
    return next({name:"Login"})
  }
  next()
})

export default router
